package lws.com.liwushuo.view.fragment.shouye;

import android.content.Context;
import android.graphics.Rect;
import android.os.Bundle;
import android.support.annotation.Nullable;
import android.support.v4.app.Fragment;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.LinearLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.ImageView;
import android.widget.LinearLayout;

import java.util.List;

import lws.com.liwushuo.R;
import lws.com.liwushuo.adapter.shouye.HotSearchAdapter;
import lws.com.liwushuo.bean.shouye.SearchHotBean;
import lws.com.liwushuo.server.shouye.SearchServer;
import retrofit2.Call;
import retrofit2.Callback;
import retrofit2.Response;
import retrofit2.Retrofit;
import retrofit2.converter.gson.GsonConverterFactory;

/**
 * Created by ZQ on 2016/10/17.
 */

public class SearchFirstFragment extends Fragment implements Callback<SearchHotBean>,HotSearchAdapter.OnItemClickListener {

    private View view;
    private RecyclerView hotSearchRecyclerView;
    private ImageView deleteAllSearchImg;
    private RecyclerView searchedListView;
    private LinearLayout bottomLinearLayout;
    private SearchServer searchServer;
    private HotSearchAdapter searchAdapter;
    private static Context context;

    @Override
    public void onCreate(@Nullable Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        context = getContext();
    }

    @Nullable
    @Override
    public View onCreateView(LayoutInflater inflater, @Nullable ViewGroup container, @Nullable Bundle savedInstanceState) {
        if (view == null) {
            view = inflater.inflate(R.layout.shouye_search_first_fragment, container, false);
            initView(view);
        }
        return view;
    }

    @Override
    public void onViewCreated(View view, @Nullable Bundle savedInstanceState) {

        //请求网络
        //http://api.liwushuo.com/v2/search/hot_words
        if (searchServer == null) {
            searchServer = new Retrofit.Builder()
                    .baseUrl("http://api.liwushuo.com/")
                    .addConverterFactory(GsonConverterFactory.create())
                    .build().create(SearchServer.class);
            searchServer.getHotBean().enqueue(this);
        }
    }

    /**
     * 初始化控件
     *
     * @param view
     */
    private void initView(View view) {
        //头部热搜词汇
        hotSearchRecyclerView = ((RecyclerView) view.findViewById(R.id.shouye_search_first_HotSearch_RecyclerView));
        GridLayoutManager layoutManager = new GridLayoutManager(getContext(), 5);
        hotSearchRecyclerView.setLayoutManager(layoutManager);
        //设置间距
        hotSearchRecyclerView.addItemDecoration(new RecyclerView.ItemDecoration() {
            @Override
            public void getItemOffsets(Rect outRect, View view, RecyclerView parent, RecyclerView.State state) {
                outRect.top = 10;
                outRect.left = 15;
                outRect.bottom = 15;
                outRect.right = 10;
            }
        });
        //底部搜索记录
        bottomLinearLayout = (LinearLayout) view.findViewById(R.id.shouye_search_first_bottom);
        //删除搜索记录
        deleteAllSearchImg = ((ImageView) view.findViewById(R.id.shouye_search_first_deleteImg));
        //搜索记录的ListView
        searchedListView = (RecyclerView) view.findViewById(R.id.shouye_search_first_searchedListView);
        LinearLayoutManager linearLayoutManager = new LinearLayoutManager(getContext());
        searchedListView.setLayoutManager(linearLayoutManager);
    }

    /**
     * 网络请求回调
     *
     * @param call
     * @param response
     */
    @Override
    public void onResponse(Call<SearchHotBean> call, Response<SearchHotBean> response) {
        SearchHotBean hotBean = response.body();
        List<String> hotWords = hotBean.getData().getHot_words();
        if (searchAdapter == null) {
            searchAdapter = new HotSearchAdapter(context, hotWords);
        }
        hotSearchRecyclerView.setAdapter(searchAdapter);
        searchAdapter.setOnItemClickListener(this);
    }

    @Override
    public void onFailure(Call<SearchHotBean> call, Throwable t) {

    }

    /**
     * 热词点击事件
     * @param v
     * @param position
     * @param keyWord
     */
    @Override
    public void onItemClick(View v, int position, String keyWord) {
        //这里要带着keyWord去跳转搜索结果
        SearchSecondFragment searchSecondFragment = new SearchSecondFragment();
        Bundle bundle = new Bundle();
        bundle.putString("keyWord",keyWord);
        searchSecondFragment.setArguments(bundle);
        getFragmentManager()
                .beginTransaction()
                .replace(R.id.shouye_search_maincontainer,searchSecondFragment)
                .commit();
    }
}
